package com.crm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.dto.FollowDto;
import com.crm.entity.*;
import com.crm.mapper.BusinessMapper;
import com.crm.mapper.CluesMapper;
import com.crm.mapper.FollowMapper;
import com.crm.mapper.UserMapper;
import com.crm.result.PageResultTow;
import com.crm.service.FollowService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FollowServiceImpl implements FollowService {
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private CluesMapper cluesMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 线索跟进上传
     * @param followDto
     */
    @Transactional
    @Override
    public void insertFollow(FollowDto followDto) {
        Follow follow = new Follow();
        BeanUtils.copyProperties(followDto,follow);
        Long maxFollowId = followMapper.selectMaxId();
        if (maxFollowId == null){
            maxFollowId = 1L;
        }else {
            maxFollowId +=1L;
        }
        follow.setId(maxFollowId);
        follow.setClueid(followDto.getClueId());
        follow.setSubjectid(followDto.getSubject());
        follow.setNexttime(followDto.getNextTime());
        follow.setNew01(0);
        followMapper.updateClueId(followDto.getClueId());
        followMapper.insertFollow(follow);
        Clue clue = new Clue();
        BeanUtils.copyProperties(followDto,clue);
        clue.setId(followDto.getClueId());
        clue.setName(followDto.getName());
        clue.setSex(followDto.getSex());
        clue.setAge(followDto.getAge());
        clue.setWeixin(followDto.getWeixin());
        clue.setQq(followDto.getQq());
        clue.setStatus(1);
        cluesMapper.updateClue(clue);
    }

    /**
     * 线索转商机
     * @param id
     */
    @Transactional
    @Override
    public void putBusiness(Long id) {
        cluesMapper.updateTransfer(id);//修改为商机
//        followMapper.deleteFollow(id);//删除跟进信息


        Business business = new Business();
        Clue clue = cluesMapper.getClueId(id);
//        Follow follow = followMapper.getFollowById(id);
        business.setClueId(id);
        business.setCreateTime(clue.getCreateTime());
        business.setName(clue.getName());
        business.setPhone(clue.getPhone());
        business.setChannel(clue.getChannel());
        business.setSex(clue.getSex());
        business.setAge(clue.getAge());
        business.setWeixin(clue.getWeixin());
        business.setQq(clue.getQq());
//        business.setSubject(follow.getSubjectid());
//        business.setOwner(userMapper.selectById(clue.getUserid()).getUserName());
        business.setStatus(clue.getStatus());
        businessMapper.insert(business);
    }

    /**
     * 查询跟进列表
     * @param clueId
     * @return
     */
    @Override
    public PageResultTow getRecordsByClueId(Long clueId) {
        List<ClueRecord> clueRecords = followMapper.selectByClueId(clueId);
        PageInfo<ClueRecord> pageInfo = new PageInfo<>(clueRecords);
        PageResultTow clueRecords1 = new PageResultTow(200,"查询成功",null,pageInfo.getTotal(),pageInfo.getList());
        return clueRecords1;
    }

    @Override
    public void hanleFakeClue(Long id, String reason, String remark) {
        Fake fake = new Fake();
        Long maxFakeId = followMapper.getMaxIdByFake();
        if (maxFakeId == null){
            maxFakeId = 1L;
        }else {
            maxFakeId +=1L;
        }
        fake.setId(maxFakeId);
        fake.setClueid(id);
        fake.setFalseReason(reason);
        Integer countFake = followMapper.countFake(id);
        if (countFake>=2){
            followMapper.deleteFake(id);//删除伪线索
            followMapper.deleteClue(id);//删除线索跟进
            cluesMapper.deleteClue(id);//删除线索
        }else if (countFake == 0){
            cluesMapper.updateFake(id);//修改为伪线索
            fake.setFalsecount(countFake+1);
        }
        fake.setNotes(remark);
        followMapper.insertFake(fake);
    }
}
